package com.venus.controller;

import com.github.pagehelper.PageInfo;
import com.venus.entity.Menu;
import com.venus.entity.Page;
import com.venus.service.MenuService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 16/9/17.
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

	@Autowired
	private MenuService menuService;

	/**
	 * 获取全部菜单
	 * @return
	 */
	@GetMapping("getAllParentNodeInfo")
	@ResponseBody
	public List<Menu> getAllParentNodeInfo(){
		return menuService.getAllParentNodeInfo();
	}

	/**
	 * 添加菜单 跳转
	 * @return
	 */
	@RequestMapping("addMenu")
	public String addMenu() {
		return "menu/addMenu";
	}

	/**
	 * 修改菜单 跳转
	 * @return
	 */
	@RequestMapping("updateMenu")
	public ModelAndView updateMenu(String id) {
		ModelAndView modelAndView = new ModelAndView("menu/updateMenu");
		Menu menu = menuService.findMenuInfoById(Integer.parseInt(id));
		List<Menu> list = menuService.getAllParentNodeInfo();
		modelAndView.addObject("menu",menu);
		modelAndView.addObject("list",list);
		return modelAndView;
	}

	/**
	 * 添加菜单 表单
	 * @param menu
	 * @return
	 */
	@ApiOperation(value="添加菜单表单", notes="添加菜单表单")
	@ApiImplicitParam(name = "menu", value = "菜单实体", required = true, dataType = "Menu")
	@RequestMapping("addMenuForm")
	@ResponseBody
	public Map<String,Object> addMenuForm(Menu menu) {
		Map<String,Object> result = new HashMap<>();
		try {
			menuService.addMenu(menu);
			result.put("result","success");
		} catch (Exception e) {
			result.put("result","failuer");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 修改菜单 表单
	 * @param menu
	 * @return
	 */
	@ApiOperation(value="修改菜单表单", notes="修改菜单表单")
	@ApiImplicitParam(name = "menu", value = "菜单实体", required = true, dataType = "Menu")
	@RequestMapping("updateMenuForm")
	@ResponseBody
	public Map<String,Object> updateMenuForm(Menu menu) {
		Map<String,Object> result = new HashMap<>();
		try {
			menuService.updateMenu(menu);
			result.put("result","success");
		} catch (Exception e) {
			result.put("result","failuer");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 分页查询菜单
	 * @param menu
	 * @param page
	 * @return
	 */
	@RequestMapping("getMenuListByPage")
	public ModelAndView getMenuListByPage(Menu menu,Page page) {
		ModelAndView modelAndView = new ModelAndView("menu/menuList");
		page.setObject(menu);
		PageInfo<Menu> pageInfo = menuService.findMenusByPageList(page);
		modelAndView.addObject("page",pageInfo);
		modelAndView.addObject("params",menu);
		modelAndView.addObject("list",pageInfo.getList());
		return modelAndView;
	}
}
